// src/api/incomeExpenseApi.js
import { apiClient } from './base'

// 基础 CRUD 操作

// 1. 分页查询收支项目
export const getIncomeExpenses = (params) => {
  console.log('incomeExpenseApi.getIncomeExpenses 参数:', params)
  return apiClient.get('/szxm', { params })
}

// 2. 创建收支项目
export const createIncomeExpense = (data) => 
  apiClient.post('/szxm', data)

// 3. 根据ID查询收支项目
export const getIncomeExpenseById = (id) => 
  apiClient.get(`/szxm/${id}`)

// 4. 更新收支项目
export const updateIncomeExpense = (id, data) => 
  apiClient.put(`/szxm/${id}`, data)

// 5. 删除收支项目
export const deleteIncomeExpense = (id) => 
  apiClient.delete(`/szxm/${id}`)

// 树状结构操作

// 1. 创建收支项目（支持树状结构）
export const createIncomeExpenseTree = (data) => 
  apiClient.post('/szxm/tree', data)

// 2. 更新收支项目（支持树状结构）
export const updateIncomeExpenseTree = (id, data) => 
  apiClient.put(`/szxm/tree/${id}`, data)

// 3. 删除收支项目（支持树状结构）
export const deleteIncomeExpenseTree = (id) => 
  apiClient.delete(`/szxm/tree/${id}`)

// 4. 获取根目录列表
export const getRootItems = () => 
  apiClient.get('/szxm/tree/root')

// 5. 根据父ID获取子目录列表
export const getChildrenByParentId = (parentId) => 
  apiClient.get('/szxm/tree/children', { params: { parentId } })

// 6. 根据树路径获取子孙目录
export const getDescendantsByTreePath = (treePath) => 
  apiClient.get('/szxm/tree/descendants', { params: { treePath } })

// 7. 根据树路径获取直接子目录
export const getDirectChildrenByTreePath = (treePath) => 
  apiClient.get('/szxm/tree/direct-children', { params: { treePath } })

// 8. 获取完整的树状结构
export const getTreeStructure = () => 
  apiClient.get('/szxm/tree/structure')

// 9. 根据ID获取树路径
export const getTreePathById = (id) => 
  apiClient.get(`/szxm/tree/path/${id}`)

// 10. 获取树状结构（简化版）
export const getTree = () => 
  apiClient.get('/szxm/tree')

// 搜索和统计操作
export const searchByStatus = (zt) => 
  apiClient.get('/szxm/search/zt', { params: { zt } })

export const countByStatus = (zt) => 
  apiClient.get('/szxm/count/zt', { params: { zt } })

// 默认导出
export default {
  getList: getIncomeExpenses,
  create: createIncomeExpense,
  getById: getIncomeExpenseById,
  update: updateIncomeExpense,
  delete: deleteIncomeExpense,
  getTree,
  createTree: createIncomeExpenseTree,
  updateTree: updateIncomeExpenseTree,
  deleteTree: deleteIncomeExpenseTree,
  getRootItems,
  getChildrenByParentId,
  getDescendantsByTreePath,
  getDirectChildrenByTreePath,
  getTreeStructure,
  getTreePathById,
  searchByStatus,
  countByStatus
} 